<?php 
require_once 'config/configuration.php';
require_once "includes/classes/Database.php";
require_once 'includes/functions.php';
require_once 'model/users.php';
require_once 'model/admindetails.php';
include_once 'model/email.php';
require_once 'model/trade.php';
require_once 'model/background_images.php';


// crone file for 20 days from submitting job system will send reminder for homeowner for give feedback


$tradeObj = new Model_TRADE();
$userObj = new Model_Users();
$adminObj = new Model_AdminDetails();
$emailObj = new Model_Email();


	//echo "Hi";	die();
		$jobArray = $userObj->getJobviewDetailsWithAccept();
		foreach($jobArray as $jobArray)
		{
		
			$expoldedate = @explode("-",$jobArray['date_added']); 
			$finaldate = date('Y-m-d',mktime(0,0,0,$expoldedate[1],$expoldedate[2]+20,$expoldedate[0]));
			$start_ts = strtotime($finaldate);
			$date2 =date('Y-m-d');
			$end_ts = strtotime($date2);
		
			$diff = $start_ts -$end_ts;
			$daysleft = round($diff / 86400);
			if($daysleft <= 0)
			{
				//echo "job ID ".$jobArray['jobid']."<br>";
				$fetchjobreviewdetArray = $userObj->getReviewdetailByFromToUIDAndJobID($jobArray['jobid'],$jobArray['userid']);

				if($fetchjobreviewdetArray['id']=='')
				{
					//echo "job ID ".$jobArray['jobid']."<br>";die();
					$fetchjobdetArray = $userObj->getJobDetailsByIdWithAccept($jobArray['jobid']);
					//echo"<pre>";print_R($fetchjobdetArray);die();
					if($fetchjobdetArray['postjob_id']!='')
					{	
						$imageObj  = new Model_Background_Images();
						$imageDetails = $imageObj->getCurrentActiveImage();
						
						$jobtype = $tradeObj->getTradeCategoryDetailsById($fetchjobdetArray['jobtype_id']);					
						$email_message = $emailObj->getEmailById(15);						

						$userdetail = $userObj->getUserDetailsByUserId($jobArray['userid']);
												
						$e_subject = $email_message['email_subject'];
						$message = $email_message['email_content']; 
						$subject = str_replace('[SITENAME]', SITENAME, $e_subject);
		
						$message = str_replace('[NAME]', $fetchjobdetArray['first_name'].' '.$fetchjobdetArray['last_name'] , $message);
						$message = str_replace('[TRADE_NAME]', $userdetail['first_name'].' '.$userdetail['last_name'] , $message);
						$message = str_replace('[JOBID]', "#PR".trim($fetchjobdetArray["postjob_id"]), $message); 
						$message = str_replace('[TRADE]', trim($fetchjobdetArray["trade_category"]), $message);
						$message = str_replace('[JOBTYPE]', trim($jobtype["trade_category"]), $message); 
						$message = str_replace('[JOBDESC]', trim($fetchjobdetArray["job_desc"]), $message); 
						$message = str_replace('[SITENAME]', SITENAME, $message);
						 
						//echo "<pre>";print_r($email_message);exit;
						// echo "<pre>";print_r($message);exit;
						
									
						
						$to =$fetchjobdetArray["email"];
						$adminEmail = $adminObj->getAdminDetails();
						$from = $adminEmail;
						$fromname= SITENAME;
								 		 
						
						$emailTemplate = file_get_contents(DOC_ROOT.'/emailtemplate.html');
						$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
						$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
						$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
							
						
						
						if($fetchjobdetArray['mail_status']=='0')
						{	
						//echo $to."<br>".$template_msg."<br>".$subject."<br>".$from."<br>".$fromname;
						//exit;
							if(trim($to) != '')
								@send_mail($to, $template_msg, $subject, $from, $fromname);	
							
							$editjobArray = array();
							$editjobArray['mail_status']=1;
							$userObj->editJobPostValueByJOBId($editjobArray, $fetchjobdetArray['postjob_id']);	
						}
						
					}
			   }
			}
		}


?>